package ru.dirty.android;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.List;

public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
	 
  
    public final ImageLoader asyncImageLoader;
    private Activity activity;
    private ListViewHolder listViewHolder;
 
    public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts, ListView listView) {
        super(activity, 0, imageAndTexts);
        asyncImageLoader = new ImageLoader(activity);
        this.activity = activity;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            convertView = inflater.inflate(R.layout.image_and_text_row, null);
            
            listViewHolder = new ListViewHolder();
            listViewHolder.textView = (TextView)   convertView.findViewById(R.id.text);
            listViewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
            listViewHolder.starsView = (ImageView) convertView.findViewById(R.id.stars);
            listViewHolder.autor = (TextView)      convertView.findViewById(R.id.autor);
            listViewHolder.comments = (TextView)   convertView.findViewById(R.id.comments);
            listViewHolder.header = (TextView)     convertView.findViewById(R.id.header);
            listViewHolder.rating = (TextView) 	   convertView.findViewById(R.id.rating);
            listViewHolder.imageProgress = (ProgressBar)convertView.findViewById(R.id.imageProgressBar);
            convertView.setTag(listViewHolder);
        
        } else {
            listViewHolder = (ListViewHolder) convertView.getTag();
        }
        ImageAndText imageAndText = getItem(position);

        String imageUrl = imageAndText.getImageUrl();
        boolean starsFlag = imageAndText.getStarsFlag(); 

	    if(imageUrl==null){
            listViewHolder.imageView.setTag(0);
	        listViewHolder.imageView.setVisibility(View.GONE);
        }
	        
	    else{
            listViewHolder.imageView.setTag(1);
	    	asyncImageLoader.DisplayImage(imageUrl, listViewHolder.imageView,true);
	        listViewHolder.imageView.setVisibility(View.VISIBLE);
	    }
	        	
        if(starsFlag)
        {
        	listViewHolder.starsView.setVisibility(View.VISIBLE);
        	listViewHolder.starsView.setImageResource(R.drawable.star);
        }
        else 
        	listViewHolder.starsView.setVisibility(View.GONE);
        
        listViewHolder.textView.setText(imageAndText.getText());
        listViewHolder.autor.setText(imageAndText.getAutor());
        listViewHolder.comments.setText(imageAndText.getComments());       
        listViewHolder.rating.setText(imageAndText.getRating());

        if(imageAndText.getHeader() == null)
        	listViewHolder.header.setVisibility(View.GONE);
        else
        {
        	listViewHolder.header.setVisibility(View.VISIBLE);
            listViewHolder.header.setText(imageAndText.getHeader());
        }
 
        return convertView;
    }
}